//
//  TEWebServiceViewController.m
//  TintExample
//
//  Created by Kevin Wu on 2/29/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TEWebServiceViewController.h"




@implementation TEWebServiceViewController

- (void)dealloc
{
    TTRelease(_webService);
    [super dealloc];
}



#pragma mark - UIViewController

- (void)loadView
{
    [super loadView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self addButtonWithTitle:@"Begin small" selector:@selector(beginSmall:) type:1];
    [self addButtonWithTitle:@"Begin big" selector:@selector(beginBig:) type:1];
    [self addButtonWithTitle:@"Begin bad url" selector:@selector(beginBadURL:) type:1];
    [self addButtonWithTitle:@"Begin unreachable url" selector:@selector(beginUnreachableURL:) type:1];
    
    [self addButtonWithTitle:@"Start" selector:@selector(start:) type:2];
    [self addButtonWithTitle:@"Start and cache" selector:@selector(startAndCache:) type:2];
    [self addButtonWithTitle:@"Refresh" selector:@selector(refresh:) type:2];
    [self addButtonWithTitle:@"Cancel" selector:@selector(cancelIt:) type:2];
    
    [self addButtonWithTitle:@"Start again" selector:@selector(startAgain:) type:3];
    [self addButtonWithTitle:@"Start and cache again" selector:@selector(startAndCacheAgain:) type:3];
    [self addButtonWithTitle:@"Refresh again" selector:@selector(refreshAgain:) type:3];
}



- (void)beginSmall:(id)sender
{
    TTRelease(_webService);
    _webService = [[TEWebService alloc] init];
    _webService.didStartSelector = @selector(aabbDidStart:);
    _webService.address = @"http://www.baidu.com/";
    [_webService addDelegate:self];
    [_webService start];
}
- (void)beginBig:(id)sender
{
    TTRelease(_webService);
    _webService = [[TEWebService alloc] init];
    _webService.address = @"http://farm6.staticflickr.com/5238/7110620063_76d694a3e6_k.jpg";
    [_webService addDelegate:self];
    [_webService start];
}
- (void)beginBadURL:(id)sender
{
    TTRelease(_webService);
    _webService = [[TEWebService alloc] init];
    _webService.address = @"blah blah";
    [_webService addDelegate:self];
    [_webService start];
}
- (void)beginUnreachableURL:(id)sender
{
    TTRelease(_webService);
    _webService = [[TEWebService alloc] init];
    _webService.address = @"http://www.facebook.com/";
    [_webService addDelegate:self];
    [_webService start];
}



- (void)start:(id)sender
{
    TTRelease(_webService);
    _webService = [[TEWebService alloc] init];
    _webService.address = @"http://www.baidu.com/";
    _webService.datetime = [NSDate date];
    _webService.weight = 115292150460600009L;
    _webService.name = @"";
    [_webService addPropertiesAsParameter:@" name,  height, adult "];
    [_webService addDelegate:self];
    [_webService start];
}
- (void)startAndCache:(id)sender
{
    [_webService startWithCacheTimeoutInterval:300];
}
- (void)refresh:(id)sender
{
    [_webService refreshWithCacheTimeoutInterval:300];
}

- (void)cancelIt:(id)sender
{
    [_webService cancel];
}



- (void)startAgain:(id)sender
{
    [_webService start];
}

- (void)startAndCacheAgain:(id)sender
{
    [_webService startWithCacheTimeoutInterval:10];
}

- (void)refreshAgain:(id)sender
{
    [_webService refreshWithCacheTimeoutInterval:10];
}




- (void)aabbDidStart:(TTWebService *)service
{
    if (service.loading) TTDPRINT(@"loading");
    else TTDPRINT(@"not loading");
}






- (void)webServiceAlreadyInCache:(TTWebService *)service
{
    if (service.loading) TTDPRINT(@"loading");
    else TTDPRINT(@"not loading");
}

- (void)webServiceDidStart:(TTWebService *)service
{
    if (service.loading) TTDPRINT(@"loading");
    else TTDPRINT(@"not loading");
}

- (void)webServiceDidFail:(TTWebService *)service withError:(NSError *)error
{
    if (service.loading) TTDPRINT(@"loading");
    else TTDPRINT(@"not loading");
}

- (void)webServiceWillFinish:(TTWebService *)service
{
    if (service.loading) TTDPRINT(@"loading");
    else TTDPRINT(@"not loading");
}

- (void)webServiceDidFinish:(TTWebService *)service
{
    if (service.loading) TTDPRINT(@"loading");
    else TTDPRINT(@"not loading");
}

@end
